//
//  FileListView.swift
//  TheWatermark
//
//  Created by sharui on 2021/6/7.
//

import SwiftUI

struct FileListView: View {
    @Binding var imageDataSource: [ImageDataSource]
    var body: some View {
        Button("添加图片") {
            FileManager.openFile { dataSource in
                if (ImageDataSource.allowAdd(imageData: dataSource, list: self.imageDataSource)) {
                    self.imageDataSource.append(dataSource)
                }
            }
        }
        GeometryReader(content: { geometry in
            List(imageDataSource, id:\.self.path) {item in
                ImageListItem(title: item.path, image: item.image, selected: item.selected)
                    .listRowInsets(.init())
                    .frame(width:geometry.size.width,height: 80)
                Divider()
            }
        })
    }
}

struct ImageListItem: View {
    var title: String
    var image: NSImage
    @State var selected: Bool
    var body: some View {
        VStack {
            HStack {
                Button(action: {
                    selected.toggle()
                }, label: {
                    ZStack {
                        Image(selected ? "list_selected" : "list_no_selected")
                    }
                }).buttonStyle(PlainButtonStyle())
                
                Image(nsImage: image)
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: 88)
                Text(title)
                Spacer()
            }
        }.frame(height: 100, alignment: .leading)
    }
}
